﻿@{
    ViewBag.Title = "InspectionBillForm";
    Layout = "~/Views/Shared/_Form.cshtml";
}

<script>
    var keyValue = request('keyValue'),
        _Type = request('Type'),
        BillCode = request('BillCode'),
        _State = 0, _ItemIds = "", Code = '@ViewBag.Code';
    $(function () {
        initControl();
    })
    function initControl() {
        //验收种类
        $("#InspectType").ComboBox({
            url: "../../SystemManage/DataItemDetail/GetDataItemListJson?EnCode=InspectionType",
            description: "==请选择==",
            id: "ItemValue",
            text: "ItemName",
            height: "110px",
            click: function (item) {
                _Type = item.attr('data-value');
            }
        });
        debugger;
        if (_Type != "") $("#InspectType").ComboBoxSetValue(_Type);
        TreeCheck();
        //获取表单
        if (!!keyValue) {
            $.SetForm({
                url: "../../PropertyManage/CS_Inspection/GetBillEntity",
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#form1").SetWebControls(data);
                    _Type = data.InspectType;
                    _State = data.State;
                    BillCode = data.BillCode;
                    TreeCheck();
                    SelectedTree();
                    if (_State == 2) {
                        $("#Memo").attr("readonly", "readonly");
                    }
                }
            });

            $("#Code").attr("readonly", "readonly");
            $("#InspectType").attr("disabled", "disabled");
        }
    }

    function TreeCheck() {
        itemurl = "../../PropertyManage/CS_Inspection/GetItemTreeList?Type=" + _Type;
        var item = {
            height: 100,
            showcheck: true,
            url: itemurl
        };
        $("#Items").treeview(item);
    }

    function SelectedTree() {
        var $img = $("#Items").find("img.bbit-tree-node-cb");
        $img.trigger("click");
        $.SetForm({
            url: "../../PropertyManage/CS_Inspection/GetBillDetailList",
            param: { BillCode: keyValue, Type: _Type },
            success: function (data) {
                $.each(data, function (i, item) {
                    _ItemIds += item.InspectionItemId + ",";
                    $("#Items").setCheckedNodeOne(item.InspectionItemId);
                    if (_State == 1) {
                        $('#Items_' + item.InspectionItemId.replace(/-/g, "_") + '_cb').click(function () {
                            dialogMsg('正在验收中,只允许增加项目', 0);
                            return false;
                        });
                    }
                });
            }
        });
        if (_State == 2) {
            $img.click(function () {
                return false;
            });
        }
    }


    //保存表单
    function AcceptClick() {
        if (!$('#form1').Validform()) {
            return false;
        }
        var postData = $("#form1").GetWebControls();
        postData["BillId"] = BillCode;
        postData["InspectType"] = _Type;
        postData["InspectTypeName"] = $("#InspectType").attr("data-text");
        postData["Code"] = '@ViewBag.Code';
        var Icodes = String($("#Items").getCheckedAllNodes());
        var Inames = "";
        for (var i = 0; i < $('#Items .bbit-tree-node-text').length; i++) {
            var datavalue = $('#Items .bbit-tree-node-text')[i].attributes[0].value;
            var innertext = $('#Items .bbit-tree-node-text')[i].innerText;
            Inames += datavalue + "_" + innertext + ",";
        }
        $.SaveForm({
            url: "../../PropertyManage/CS_Inspection/SaveBillForm?keyValue=" + keyValue,
            param: { inspectionBillEntity: JSON.stringify(postData), Icodes: Icodes, Inames: Inames, ItemIds: _ItemIds, DecorateBillId: BillCode },
            loading: "正在保存数据...",
            success: function () {
                if (!top.Form.$("#gridtable2"))
                    top.frames["Form"].$("#gridtable6").trigger("reloadGrid");
                else
                    top.Form.$("#gridtable2").trigger("reloadGrid");
            }
        })
    }

</script>

<div style="margin-left: 10px; margin-top: 20px; margin-right: 30px;">
    <table class="form">
        <tr>
            <th class="formTitle">验收单号<font face="宋体">*</font></th>
            <td class="formValue">
                <input id="Code" readonly type="text" value="@ViewBag.Code" class="form-control" />
            </td>
        </tr>
        <tr>
            <th class="formTitle">验收种类<font face="宋体">*</font></th>
            <td class="formValue">
                <div id="InspectType" readonly type="select" class="ui-select"></div>
            </td>
        </tr>
        <tr>
            <th class="formTitle">验收项目<font face="宋体">*</font></th>
            <td class="formValue">
                <div id="Items" style="border:1px solid #cecece;height:28px;"></div>
            </td>
        </tr>
        <tr>
            <th class="formTitle" valign="top" style="padding-top: 4px;">
                备注
            </th>
            <td class="formValue">
                <textarea id="Memo" class="form-control" style="height: 70px;"></textarea>
            </td>
        </tr>
    </table>
</div>

